
// ******************************
// REVISION 2020 for the RESTAT
// 
// This file contains all reduced-form regressions

// requires packages (from SSC):
// center
// reghdfe
// estout
// labutils
// gtools
// ftools
// ppml_panel_sg + hdfe

// ATTENTION: set this to the root directory before running the file
global root \\tsclient\jboehm\Dropbox\cenf\data\final

clear
clear matrix
capture set memory 10g
capture set maxvar 32767
capture set matsize 11000

cd ${root}

set more off

capture log close
capture log using run.log, append

qui do prepare.do

// ************************
// ************************
// 1.) WITH DELTA (without time)
// ************************
// ************************

// ************************
// BASELINE
// ************************
// standardize all variables
center fraction int_n_div_gm int_n_div_f, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm   , absorb(updown countrycode) vce(cluster countrycode)
estadd local countryfe "Yes"
estadd local updownfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f   , absorb(updown countrycode) vce(cluster countrycode) 
estadd local countryfe "Yes"
estadd local updownfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_gm , absorb(updown upcountry countrycode) vce(cluster countrycode)
estadd local updownfe "Yes" 
estadd local upfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f , absorb(updown upcountry countrycode) vce(cluster countrycode)
estadd local updownfe "Yes" 
estadd local upfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_gm , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F) 
qui do labelvariables.do
esttab using tables\benchmark.tex, nonotes noisily beta(a2) se(a2) scalars("countryfe Country FE" "updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// do the last two regressions with just normalizing the interaction (mention magnitude in the text)
center int_n_div_gm int_n_div_f, standardize casewise
eststo clear
eststo : quietly reghdfe fraction c_int_n_div_gm , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
di "A one SD decrease in the interaction term is associated with a decrease in the input share by `=100*_b[c_int_n_div_gm]' percentage points"
eststo : quietly reghdfe fraction c_int_n_div_f , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
di "A one SD decrease in the interaction term is associated with a decrease in the input share by `=100*_b[c_int_n_div_f]' percentage points"
esttab, r2 se 
drop c_*

// ****************************
// WITHIN M ONLY, WITHIN S ONLY
// ****************************
eststo clear
center fraction int_n_div_gm int_n_div_f if upstream<=21 & downstream<=21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm if upstream<=21 & downstream<=21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
eststo : quietly reghdfe c_fraction c_int_n_div_f if upstream<=21 & downstream<=21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
drop c_*
center fraction int_n_div_gm int_n_div_f if upstream>21 & downstream>21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm if upstream>21 & downstream>21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "S only"
eststo : quietly reghdfe c_fraction c_int_n_div_f if upstream>21 & downstream>21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "S only"
esttab, r2 se scalars(F)
qui do labelvariables.do
esttab using tables\monly_sonly.tex, nonotes noisily b(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "sample \midrule Sample" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// **********************************************
// WITH FINANCIAL DEVELOPMENT / GDPC INTERACTION
// **********************************************
// standardize all variables
center fraction int_n_div_gm int_n_div_f intf_gm intf_f, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_intf_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_intf_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_*
center fraction int_n_div_gm int_n_div_f int_lgdpc_gm int_lgdpc_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_lgdpc_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_lgdpc_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_*
center fraction int_n_div_gm int_n_div_f intf_gm intf_f int_lgdpc_gm int_lgdpc_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_intf_gm c_int_lgdpc_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_intf_f c_int_lgdpc_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F) order(c_int_n_div_gm c_int_n_div_f c_intf_gm c_intf_f c_int_lgdpc_gm c_int_lgdpc_f )
qui do labelvariables.do
esttab using tables\withfindev.tex, nonotes noisily b(a2) se(a2) order(c_int_n_div_gm c_int_n_div_f c_intf_gm c_intf_f c_int_lgdpc_gm c_int_lgdpc_f ) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*


// ****************************************
// QUANTILES
// ****************************************
// delta
center fraction int_q2_gm int_q3_gm int_q4_gm int_q5_gm, standardize casewise
eststo clear 
rename c_int_q2_gm d_int_q2_gm
rename c_int_q3_gm d_int_q3_gm
rename c_int_q4_gm d_int_q4_gm
rename c_int_q5_gm d_int_q5_gm
eststo : quietly reghdfe c_fraction d_int_q2_gm d_int_q3_gm d_int_q4_gm d_int_q5_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_*
center fraction int_q2_f int_q3_f int_q4_f int_q5_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_q2_f c_int_q3_f c_int_q4_f c_int_q5_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_fraction
center fraction int_q3gm_delta int_q4gm_delta int_q5gm_delta , standardize casewise
rename c_int_q3gm_delta d_int_q3gm_delta
rename c_int_q4gm_delta d_int_q4gm_delta
rename c_int_q5gm_delta d_int_q5gm_delta
eststo : quietly reghdfe c_fraction d_int_q3gm_delta d_int_q4gm_delta d_int_q5gm_delta, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_fraction
center fraction int_q3f_delta int_q4f_delta int_q5f_delta, standardize casewise
eststo : quietly reghdfe c_fraction c_int_q3f_delta c_int_q4f_delta c_int_q5f_delta, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
qui do labelvariables.do
esttab, r2 se 
esttab using tables\quantiles.tex, nonotes noisily beta(a2) se(a2) nonumber rename(d_int_q2_gm c_int_q2_f d_int_q3_gm c_int_q3_f d_int_q4_gm c_int_q4_f d_int_q5_gm c_int_q5_f d_int_q3gm_delta c_int_q3f_delta d_int_q4gm_delta c_int_q4f_delta d_int_q5gm_delta c_int_q5f_delta) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles posthead(" &\multicolumn{4}{c}{Dependent variable: \$X\_{ni}/X\_n\$} \\\cmidrule(lr){2-5} &\multicolumn{1}{c}{Using \$z_{ni}^{(1)}\$}&\multicolumn{1}{c}{Using \$z_{ni}^{(2)}\$}&\multicolumn{1}{c}{Using \$z_{ni}^{(1)}\$}&\multicolumn{1}{c}{Using \$z_{ni}^{(2)}\$}\\\cmidrule(lr){2-2}\cmidrule(lr){3-3}\cmidrule(lr){4-4}\cmidrule(lr){5-5} &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)} \\ \midrule") 
drop c_* d_*


// ******************************
// DOMESTIC I-O SHARES
// ******************************
// standardize all variables
center domfraction int_n_div_gm int_n_div_f, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_domfraction c_int_n_div_gm , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_domfraction c_int_n_div_f , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
qui do labelvariables.do
esttab, r2 se 
esttab using tables\domestic.tex, nonotes noisily beta(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}^{\text{dom}}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*


// ******************************
//  EXCLUDING THE TOP 3 SECTORS
// ******************************
// standardize all variables
center fraction int_n_div_gm int_n_div_f if upstream<31 | upstream>33, standardize casewise
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm if upstream<31 | upstream>33, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Excl. I/F/B"
eststo : quietly reghdfe c_fraction c_int_n_div_f if upstream<31 | upstream>33, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Excl. I/F/B"
drop c_*
center fraction int_n_div_gm int_n_div_f int_usio, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_usio, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_usio, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
esttab, r2 se scalars(F)
qui do labelvariables.do
esttab using tables\excludingtop3usio.tex, nonotes noisily beta(a2) se(a2) order(c_int_n_div_gm c_int_n_div_f c_int_usio) scalars("sample Sample" "updownfe \midrule Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// ****************************************
// USING THE RAUCH CLASSIFICATION OF GOODS
// ****************************************
gen intf_rauch_con = findev * (1-con)
gen intf_rauch_lib = findev * (1-lib)
gen int_lgdpc_rauch_con = loggdpc * (1-con)
gen int_lgdpc_rauch_lib = loggdpc * (1-lib)

center fraction int_rauch_con int_rauch_lib , standardize casewise
eststo clear 
eststo : quietly reghdfe c_fraction c_int_rauch_con , absorb(updown downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes" 
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_rauch_lib , absorb(updown downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes" 
estadd local downfe "Yes"
drop c_*
center fraction int_rauch_con int_rauch_lib intf_rauch_con intf_rauch_lib int_lgdpc_rauch_con int_lgdpc_rauch_lib, standardize casewise
eststo : quietly reghdfe c_fraction c_int_rauch_con  c_intf_rauch_con , absorb(updown downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes" 
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_rauch_lib  c_intf_rauch_lib , absorb(updown downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes" 
estadd local downfe "Yes"
drop c_*
center fraction int_rauch_con int_rauch_lib intf_rauch_con intf_rauch_lib int_lgdpc_rauch_con int_lgdpc_rauch_lib, standardize casewise
eststo : quietly reghdfe c_fraction c_int_rauch_con  c_intf_rauch_con c_int_lgdpc_rauch_con, absorb(updown downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes" 
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_rauch_lib  c_intf_rauch_lib c_int_lgdpc_rauch_lib, absorb(updown downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes" 
estadd local downfe "Yes"
qui do labelvariables.do
esttab, r2 se
esttab using tables/rauch2.tex, nonotes noisily beta(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// ****************************************
// HETEROGENEITY ACROSS DOWNSTREAM SECTORS
// ****************************************
gen int3_gm_con = int_n_div_gm * (1-con_down)
gen int3_gm_lib = int_n_div_gm * (1-lib_down)
gen int3_f_con = int_n_div_f * (1-con_down)
gen int3_f_lib = int_n_div_f * (1-lib_down)

center fraction int_n_div_gm int_n_div_f int3_gm_lib int3_f_lib, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int3_gm_lib, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int3_f_lib, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F)
qui do labelvariables.do 
esttab using tables\downstream-hetero.tex, nonotes noisily beta(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// ************************************************
// DIFFERENT Z-MEASURES
// ************************************************
center fraction int_n_div_gm_sellerptf int_n_div_f_sellerptf, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm_sellerptf , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f_sellerptf , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_fraction
center fraction int_casesPerLinkJpn, standardize casewise
eststo : quietly reghdfe c_fraction c_int_casesPerLinkJpn , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F)
qui do labelvariables.do
esttab using tables\robustness2.tex, nonotes noisily beta(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// ************************
// USING JAPANESE LINKAGES
// ************************
// standardize all variables
center fraction int_casesPerLinkJpn if upstream!=3 & downstream!=3, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_casesPerLinkJpn if upstream!=3 & downstream!=3, absorb(updown upcountry countrycode) vce(cluster countrycode) 
eststo : quietly reghdfe c_fraction c_int_casesPerLinkJpn if upstream!=3 & downstream!=3, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
esttab, r2 se scalars(F)
drop c_*
eststo clear
eststo : quietly reghdfe fraction int_casesPerLinkJpn if upstream!=3 & downstream!=3, absorb(updown upcountry countrycode) vce(cluster countrycode) 
eststo : quietly reghdfe fraction int_casesPerLinkJpn if upstream!=3 & downstream!=3, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
esttab, r2 se scalars(F)
eststo clear
eststo : quietly reghdfe fraction int_casesPerLink if upstream!=3 & downstream!=3, absorb(updown upcountry countrycode) vce(cluster countrycode) 
eststo : quietly reghdfe fraction int_casesPerLink if upstream!=3 & downstream!=3, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
esttab, r2 se scalars(F)

// ************************************************
// CASES PER DOLLAR IN US IO TABLE
// ************************************************
// if i use #cases/fraction_US, the estimate is not significant. that's because
// measurement error in the small IO shares will blow up the resulting measure.
// However, if I restrict attention to 'important' sector pairs (IO share>1%)
// then the estimates are negative and significant again.
// standardize all variables
center fraction int_n_div_usio, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_usio , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
drop c_*
center fraction int_n_div_usio if usio>0.015, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_usio if usio>0.015, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
esttab, r2 se scalars(F)
esttab using tables\usio.tex, r2 se scalars(F)  replace
drop c_*

// *******************************
// WITH DELTA THAT INCLUDES TIME
// *******************************
eststo clear
center fraction int_t_n_div_gm int_t_n_div_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_t_n_div_gm , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_t_n_div_f , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_*
center fraction int_t_n_div_gm int_t_n_div_f intf_gm intf_f int_lgdpc_gm int_lgdpc_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_t_n_div_gm c_intf_gm c_int_lgdpc_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_t_n_div_f c_intf_f c_int_lgdpc_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F)
qui do labelvariables.do
esttab using tables\robustness_t.tex, nonotes noisily beta(a2) se(a2) scalars("countryfe Country FE" "updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*


// *******************************
// CONTROLLING FOR NUNN X RAUCH INTERACTIONS
// *******************************
clear
qui do prepare.do

gen rauch_con = 1-con
gen rauch_lib = 1-lib

// Nunn is weighted average of Rauch
bysort countrycode downstream (upstream): egen sumf = total(fraction)
gen rwt_con = (fraction/sumf) *rauch_con
gen rwt_lib = (fraction/sumf)*rauch_lib
bysort countrycode downstream (upstream): egen nunn_con = total(rwt_con) if countrycode==840
bysort countrycode downstream (upstream): egen nunn_lib = total(rwt_lib) if countrycode==840
bysort downstream upstream (nunn_con) : replace nunn_con = nunn_con[1]
bysort downstream upstream (nunn_lib) : replace nunn_lib = nunn_lib[1]
drop rwt_*

gen int_new_con = delta* nunn_con * rauch_con
gen int_new_lib = delta* nunn_lib * rauch_lib

center fraction int_n_div_gm int_n_div_f int_new_con int_new_lib, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_new_con, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_new_con, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_new_lib, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_new_lib, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F) 
qui do labelvariables.do
esttab using tables/nunn-control.tex, nonotes noisily beta(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*


// *******************************
// PPML
// *******************************
// standardize to SD of one
cap drop c_*
foreach var in fraction int_n_div_gm int_n_div_f {
	egen sd_`var' = sd(`var')
	gen c_`var' = `var'/sd_`var'
	drop sd_`var'
}
eststo clear
eststo : quietly ppml_panel_sg c_fraction c_int_n_div_gm , ex(upstream) im(downstream) year(countrycode) cluster(countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local method "PPML"
eststo : quietly ppml_panel_sg c_fraction c_int_n_div_f , ex(upstream) im(downstream) year(countrycode) cluster(countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local method "PPML"
esttab, r2 se scalars(F) 
do labelvariables.do
esttab using tables/ppml.tex, nonotes se noisily scalars("method Estimator" "updownfe \midrule Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
cap drop c_*

// *******************************
// CAPITAL INTENSITY AND R&D INTERACTIONS
// *******************************
gen int_n_div_gm_cap = int_n_div_gm * cap_intensity
gen int_n_div_f_cap = int_n_div_f * cap_intensity
gen int_n_div_gm_rnd = int_n_div_gm * rnd_intensity
gen int_n_div_f_rnd = int_n_div_f * rnd_intensity
// baseline
center fraction int_n_div_gm int_n_div_f int_n_div_gm_cap int_n_div_f_cap, standardize casewise
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_n_div_gm_cap, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_n_div_f_cap, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_*
center fraction int_n_div_gm int_n_div_gm_cap int_n_div_f int_n_div_f_cap intf_gm intf_f int_lgdpc_gm int_lgdpc_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_n_div_gm_cap c_intf_gm c_int_lgdpc_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_n_div_f_cap c_intf_f c_int_lgdpc_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_fraction c_int_n_div_gm c_intf_gm c_int_lgdpc_gm ///
	c_fraction c_int_n_div_f c_intf_f c_int_lgdpc_f
center fraction int_n_div_gm int_n_div_f int_n_div_gm_rnd int_n_div_f_rnd, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_n_div_gm_rnd, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_n_div_f_rnd, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
drop c_fraction c_int_n_div_gm c_int_n_div_gm_rnd c_int_n_div_f c_int_n_div_f_rnd
center fraction int_n_div_gm int_n_div_gm_rnd int_n_div_f int_n_div_f_rnd intf_gm intf_f int_lgdpc_gm int_lgdpc_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_int_n_div_gm_rnd c_intf_gm c_int_lgdpc_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_int_n_div_f_rnd c_intf_f c_int_lgdpc_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F) 
qui do labelvariables.do
esttab using tables\robustness-cap-rnd.tex, nonotes noisily beta(a2) se(a2) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// *********************************************************************
// CONTROLLING FOR SCALE -- REF 3.1
// *********************************************************************

gen control_gm = delta *  sqrt(numberOfFirmsOrbisUpstream*numberOfFirmsOrbisDownstream)
gen control_f = delta * exp(fittedlinks_us) // fittedlinks_us is log fitted links

// baseline
center fraction int_n_div_gm int_n_div_f control_gm control_f, standardize casewise
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_control_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_control_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
esttab, r2 se scalars(F) 
drop c_*
center fraction int_n_div_gm int_n_div_f control_gm control_f if upstream<=21 & downstream<=21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_control_gm if upstream<=21 & downstream<=21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_control_f if upstream<=21 & downstream<=21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
drop c_*
center fraction int_n_div_gm int_n_div_f control_gm control_f if upstream>21 & downstream>21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_control_gm if upstream>21 & downstream>21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "S only"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_control_f if upstream>21 & downstream>21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "S only"
drop c_*
center fraction int_n_div_gm int_n_div_f intf_gm intf_f int_lgdpc_gm int_lgdpc_f control_gm control_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_gm c_intf_gm c_int_lgdpc_gm c_control_gm, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
eststo : quietly reghdfe c_fraction c_int_n_div_f c_intf_f c_int_lgdpc_f c_control_f, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
qui do labelvariables.do
esttab, r2 se scalars(F) order(c_int_n_div_gm c_int_n_div_f c_intf_gm c_intf_f c_int_lgdpc_gm c_int_lgdpc_f )
esttab using tables\robustness-scale.tex, drop(*cons*) nonotes noisily b(a2) se(a2) order(c_int_n_div_gm c_int_n_div_f c_intf_gm c_intf_f c_int_lgdpc_gm c_int_lgdpc_f ) scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "sample \midrule Sample" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )

drop c_*

// USING Z^3 and Z^4

gen int_n_div_usiot = n_div_usiot * delta
gen int_n_div_sales = n_div_sales * delta

// financial development and z_ni
gen intf3 = findev * n_div_sales
gen intf4 = findev * n_div_usiot
// log gdp/capita and z_ni
gen int_lgdpc3 = loggdpc*n_div_sales
gen int_lgdpc4 = loggdpc*n_div_usiot

// z^3
center fraction int_n_div_sales  , standardize casewise
eststo clear
eststo : quietly reghdfe c_fraction c_int_n_div_sales , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
drop c_*
center fraction int_n_div_sales if upstream<=21 & downstream<=21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_sales if upstream<=21 & downstream<=21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
drop c_*
center fraction int_n_div_sales if upstream>21 & downstream>21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_sales if upstream>21 & downstream>21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "S only"
drop c_*
center fraction int_n_div_sales intf3 int_lgdpc3 , standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_sales c_intf3 c_int_lgdpc3 , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
qui do labelvariables.do
esttab, r2 se
drop c_fraction
// z^4
center fraction int_n_div_usiot  , standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_usiot , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
drop c_fraction c_int_n_div_usiot
center fraction int_n_div_usiot if upstream<=21 & downstream<=21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_usiot if upstream<=21 & downstream<=21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
drop c_fraction c_int_n_div_usiot
center fraction int_n_div_usiot if upstream>21 & downstream>21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_usiot if upstream>21 & downstream>21, absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "S only"
drop c_fraction c_int_n_div_usiot
center fraction int_n_div_usiot intf4 int_lgdpc4 , standardize casewise
eststo : quietly reghdfe c_fraction c_int_n_div_usiot c_intf4 c_int_lgdpc4 , absorb(updown upcountry downcountry countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "Full"
qui do labelvariables.do
esttab, r2 se scalars(F) 
esttab using tables\robustness-scale2.tex, drop(*cons*) nonotes noisily b(a2) se(a2)  scalars("updownfe Upstream \$\times\$ Downstream FEs" "upfe Upstream \$\times\$ Country FEs" "downfe Downstream \$\times\$ Country FEs" "sample \midrule Sample" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*



// *********************************************************************
// BREAKING Z DOWN INTO UPSTREAM/DOWNSTREAM VARIATION -- REF 3.2 
// *********************************************************************
bysort countrycode upstream	(downstream): egen z1_up = median(n_div_gm)
bysort countrycode upstream	(downstream): egen z2_up = median(n_div_f)
bysort countrycode downstream	(upstream): egen z1_down = median(n_div_gm)
bysort countrycode downstream	(upstream): egen z2_down = median(n_div_f)
gen int_z1_up = delta*z1_up
gen int_z2_up = delta*z2_up
gen int_z1_down = delta*z1_down
gen int_z2_down = delta*z2_down
// financial development and z_ni
gen intf_z1_up=findev*z1_up
gen intf_z2_up=findev*z2_up
gen intf_z1_down=findev*z1_down
gen intf_z2_down=findev*z2_down
// log gdp/capita and z_ni
gen int_lgdpc_z1_up = loggdpc*z1_up
gen int_lgdpc_z2_up = loggdpc*z2_up
gen int_lgdpc_z1_down = loggdpc*z1_down
gen int_lgdpc_z2_down = loggdpc*z2_down
// quantiles
xtile qtind=delta, nq(5)
forvalues i=1/5 {
gen int_q`i'_z1_up = (qtind==`i')*z1_up
gen int_q`i'_z2_up = (qtind==`i')*z2_up
gen int_q`i'_z1_down = (qtind==`i')*z1_down
gen int_q`i'_z2_down = (qtind==`i')*z2_down
}
drop qtind


// benchmark
// standardize all variables
center fraction int_z1_up int_z2_up int_z1_down int_z2_down, standardize casewise
// run
eststo clear
eststo : quietly reghdfe c_fraction c_int_z1_up  c_int_z1_down , absorb(updown countrycode) vce(cluster countrycode)
estadd local countryfe "Yes"
estadd local updownfe "Yes"
eststo : quietly reghdfe c_fraction c_int_z2_up  c_int_z2_down , absorb(updown countrycode) vce(cluster countrycode) 
estadd local countryfe "Yes"
estadd local updownfe "Yes" 
drop c_*
// M only, S only
center fraction int_z1_up int_z2_up int_z1_down int_z2_down if upstream<=21 & downstream<=21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_z1_up  c_int_z1_down if upstream<=21 & downstream<=21, absorb(updown countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local countryfe "Yes"
estadd local sample "M only"
eststo : quietly reghdfe c_fraction c_int_z2_up  c_int_z2_down if upstream<=21 & downstream<=21, absorb(updown countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
estadd local sample "M only"
drop c_*
center fraction int_z1_up int_z2_up int_z1_down int_z2_down if upstream>21 & downstream>21, standardize casewise
eststo : quietly reghdfe c_fraction c_int_z1_up  c_int_z1_down if upstream>21 & downstream>21, absorb(updown countrycode) vce(cluster countrycode) 
estadd local countryfe "Yes"
estadd local updownfe "Yes" 
estadd local sample "S only"
eststo : quietly reghdfe c_fraction c_int_z2_up  c_int_z2_down if upstream>21 & downstream>21, absorb(updown countrycode) vce(cluster countrycode) 
estadd local countryfe "Yes"
estadd local updownfe "Yes" 
estadd local sample "S only"
drop c_*
center fraction int_z1_up int_z2_up int_z1_down int_z2_down ///
	intf_z1_up intf_z1_down int_lgdpc_z1_up int_lgdpc_z1_down ///
	intf_z2_up intf_z2_down int_lgdpc_z2_up int_lgdpc_z2_down ///
	, standardize casewise
eststo : quietly reghdfe c_fraction c_int_z1_up  c_int_z1_down c_intf_z1_up c_intf_z1_down c_int_lgdpc_z1_up c_int_lgdpc_z1_down, absorb(updown countrycode) vce(cluster countrycode) 
estadd local countryfe "Yes"
estadd local updownfe "Yes" 
eststo : quietly reghdfe c_fraction c_int_z2_up  c_int_z2_down c_intf_z2_up c_intf_z2_down c_int_lgdpc_z2_up c_int_lgdpc_z2_down, absorb(updown countrycode) vce(cluster countrycode) 
estadd local countryfe "Yes"
estadd local updownfe "Yes" 
esttab, r2 se scalars(F)
qui do labelvariables.do
esttab using tables\robustness-ref32.tex, drop(*cons*) nonotes noisily b(a2) se(a2)  scalars("updownfe Upstream \$\times\$ Downstream FEs" "countryfe Country FEs" "sample \midrule Sample" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles mgroups("Dependent variable: \$X\_{ni}/X\_n\$", pattern(1 0 0 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})  )
drop c_*

// z_ni
center fraction int_z1_up int_z2_up int_z1_down int_z2_down intf_gm intf_f int_lgdpc_gm int_lgdpc_f, standardize casewise
eststo : quietly reghdfe c_fraction c_int_z1_up  c_int_z1_down c_intf_gm c_int_lgdpc_gm, absorb(updown countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
eststo : quietly reghdfe c_fraction c_int_z2_up  c_int_z2_down c_intf_f c_int_lgdpc_f, absorb(updown countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local upfe "Yes"
estadd local downfe "Yes"
esttab, r2 se scalars(F)
drop c_*

// quantiles
eststo clear
center fraction int_q2_z1_up int_q2_z1_down int_q2_z2_up int_q2_z2_down int_q3_z1_up int_q3_z1_down int_q3_z2_up int_q3_z2_down ///
	int_q4_z1_up int_q4_z1_down int_q4_z2_up int_q4_z2_down int_q5_z1_up int_q5_z1_down int_q5_z2_up int_q5_z2_down, standardize casewise
eststo clear 
forval quant = 2/5 {
	rename c_int_q`quant'_z1_up d_int_q`quant'_z1_up
	rename c_int_q`quant'_z2_up d_int_q`quant'_z2_up
	rename c_int_q`quant'_z1_down d_int_q`quant'_z1_down
	rename c_int_q`quant'_z2_down d_int_q`quant'_z2_down
	
}
eststo : quietly reghdfe c_fraction d_int_q2_z1_up d_int_q2_z1_down d_int_q3_z1_up d_int_q3_z1_down ///
	 d_int_q4_z1_up d_int_q4_z1_down d_int_q5_z1_up d_int_q5_z1_down, absorb(updown countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local countryfe "Yes"
eststo : quietly reghdfe c_fraction d_int_q2_z2_up d_int_q2_z2_down d_int_q3_z2_up d_int_q3_z2_down ///
	 d_int_q4_z2_up d_int_q4_z2_down d_int_q5_z2_up d_int_q5_z2_down, absorb(updown countrycode) vce(cluster countrycode) 
estadd local updownfe "Yes"
estadd local countryfe "Yes"
qui do labelvariables.do
esttab, r2 se rename(d_int_q2_z2_up d_int_q2_z1_up d_int_q3_z2_up d_int_q3_z1_up d_int_q4_z2_up d_int_q4_z1_up d_int_q5_z2_up d_int_q5_z1_up d_int_q2_z2_down d_int_q2_z1_down d_int_q3_z2_down d_int_q3_z1_down d_int_q4_z2_down d_int_q4_z1_down d_int_q5_z2_down d_int_q5_z1_down )
esttab using tables\robustness-ref32-quantiles.tex, nonotes noisily beta(a2) se(a2) nonumber rename(d_int_q2_z2_up d_int_q2_z1_up d_int_q3_z2_up d_int_q3_z1_up d_int_q4_z2_up d_int_q4_z1_up d_int_q5_z2_up d_int_q5_z1_up d_int_q2_z2_down d_int_q2_z1_down d_int_q3_z2_down d_int_q3_z1_down d_int_q4_z2_down d_int_q4_z1_down d_int_q5_z2_down d_int_q5_z1_down ) scalars("updownfe Upstream \$\times\$ Downstream FEs" "countryfe Country FEs" "r2 \midrule\$R^2\$" "N \$N\$") obslast label booktabs replace  substitute(\_ _) nomtitles posthead(" &\multicolumn{2}{c}{Dep. var.: \$X\_{ni}/X\_n\$} \\\cmidrule(lr){2-3} &\multicolumn{1}{c}{Using \$z_{ni}^{(1)}\$}&\multicolumn{1}{c}{Using \$z_{ni}^{(2)}\$}\\\cmidrule(lr){2-2}\cmidrule(lr){3-3} &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)} \\ \midrule") 
drop c_* d_*

// ************************************************************************
// OTHER (GRAPHS, AUXILIARY TABLES ETC)
// ************************************************************************

do other.do

capture log close

